package com.awspaas.user.apps.cpq.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.commons.mvc.view.ResponseObject;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.ibm.db2.jcc.am.bp;

public class CPQ_GetSystemPriceWeb {

	public String getSystemPrice(UserContext userContext, String bindId,String type) {
		try {
			String clfSql="SUM(ZCLF)";
			String zjSql="SUM(ZJ)";									
			String sql="select XTMC,XTMH,PX,"+clfSql+" as CLF, SUM(ZCGF) as CGF,"
					+ "SUM(ZGSF) as RGF,SUM(ZWX) as WX,"+zjSql+" as DJ "
					+ "from  BO_EU_CPQ_PROJECT_MSG_GRIDS "
					+ "where  BINDID=? GROUP BY XTMC ";
			String zsql="select XTMC,"+clfSql+" as CLF, SUM(ZCGF) as CGF,"
					+ "SUM(ZGSF) as RGF,SUM(ZWX) as WX,"+zjSql+" as DJ "
					+ "from  BO_EU_CPQ_PROJECT_MSG_DZ "
					+ "where  BINDID=? GROUP BY XTMC ";
			String sqlPx=" select XTMC,XTMH,PX from  BO_EU_CPQ_SYSTEM_MSG ";
			//删除原有的单栋设备明细信息数据
			List<BO> bos=	SDK.getBOAPI().query("BO_EU_CPQ_SYSTEM_PRICE").bindId(bindId).list();
			Map<String, BO> mapOld= new HashMap<String, BO>();
			if(bos.size()>0) {
				for (BO bo : bos) {
					mapOld.put(bo.getString("XTMC"),bo);
				}
				
			}						
			List<RowMap> maps=DBSql.getMaps(sql, new Object[] {bindId});
			List<RowMap> pxmap=DBSql.getMaps(sqlPx);
			Map<String, String>  px=new HashMap<String, String>();
			if(pxmap.size()>0) {
				for (RowMap rowMap : pxmap) {
					px.put(rowMap.getString("XTMC"), rowMap.getString("PX"));
				}
			}
			//查询定制信息的价格
			List<RowMap> zmaps=DBSql.getMaps(zsql, new Object[] {bindId});
			//将定制信息的价格存放到map集合中
			Map<String, RowMap> dzMap=new HashMap<>();
			if(zmaps.size()>0) {
				for (RowMap rowMap : zmaps) {
					dzMap.put(rowMap.getString("XTMC"), rowMap);
				}
			}
			Map<String, RowMap> xtMap=new HashMap<>();
			if(maps.size()>0) {
				for (RowMap rowMap : maps) {
					xtMap.put(rowMap.getString("XTMC"), rowMap);
				}
			}
			if(maps.size()>0) {
				List<BO> lists=new ArrayList<BO>();
				List<String> names=new ArrayList<String>();
				for (RowMap rowMap : maps) {
					
					String name=rowMap.getString("XTMC");
					names.add(name);
					String clf=rowMap.getString("CLF").isEmpty()?"0":rowMap.getString("CLF");
					String rgf=rowMap.getString("RGF").isEmpty()?"0":rowMap.getString("RGF");
					String wx=rowMap.getString("WX").isEmpty()?"0":rowMap.getString("WX");
					String cgf=rowMap.getString("CGF").isEmpty()?"0":rowMap.getString("CGF");
					String dj=rowMap.getString("DJ").isEmpty()?"0":rowMap.getString("DJ");
					//判断有没有该系统的定制信息，如果有则进行相加；
					if(dzMap.containsKey(name)) {
						RowMap map=dzMap.get(name);
						String zclf=map.getString("CLF").isEmpty()?"0":map.getString("CLF");
						String zrgf=map.getString("RGF").isEmpty()?"0":map.getString("RGF");
						String zwx=map.getString("WX").isEmpty()?"0":map.getString("WX");
						String zcgf=map.getString("CGF").isEmpty()?"0":map.getString("CGF");
						String zdj=map.getString("DJ").isEmpty()?"0":map.getString("DJ");
						BigDecimal clfnum1=new BigDecimal(clf);	
						BigDecimal rgfnum1=new BigDecimal(rgf);		
						BigDecimal wxfnum1=new BigDecimal(wx);	
						BigDecimal cgfnum1=new BigDecimal(cgf);
						BigDecimal djnum1=new BigDecimal(dj);
						BigDecimal clfnum2=new BigDecimal(zclf);	
						BigDecimal rgfnum2=new BigDecimal(zrgf);		
						BigDecimal wxfnum2=new BigDecimal(zwx);	
						BigDecimal cgfnum2=new BigDecimal(zcgf);
						BigDecimal djnum2=new BigDecimal(zdj);
						clf=clfnum1.add(clfnum2).toString();
						rgf=rgfnum1.add(rgfnum2).toString();
						wx=wxfnum1.add(wxfnum2).toString();
						cgf=cgfnum1.add(cgfnum2).toString();
						dj=djnum1.add(djnum2).toString();
					}
					  BigDecimal clfnum=new BigDecimal(clf);	
					  BigDecimal rgfnum=new BigDecimal(rgf);		
					  BigDecimal wxfnum=new BigDecimal(wx);	
					  BigDecimal cgfnum=new BigDecimal(cgf);
					  BigDecimal djnum=new BigDecimal(dj);
					if(mapOld.containsKey(name)) {
					  BO oldBo=mapOld.get(name);
					  oldBo.set("XTMC", rowMap.getString("XTMC"));
					  oldBo.set("XTMH", rowMap.getString("XTMH"));
					  oldBo.set("PX", px.get(rowMap.getString("XTMC")));					  
					  oldBo.set("CLF",clf);
					  oldBo.set("RGF",rgf);
					  oldBo.set("WXWG", wx);
					  oldBo.set("CGF", cgf);
					  oldBo.set("DJ", dj);
					  String zzxs=oldBo.getString("ZZXS").isEmpty()?"0":oldBo.getString("ZZXS");//自制系数
					  String wgxs=oldBo.getString("WGXS").isEmpty()?"0":oldBo.getString("WGXS");//外购系数
					
					  BigDecimal zznum=new BigDecimal(zzxs);
					  BigDecimal wgnum=new BigDecimal(wgxs);
					  BigDecimal zdj=clfnum.add(rgfnum).multiply(zznum).add(wxfnum.add(cgfnum).multiply(wgnum));
					  oldBo.set("BJDJG", zdj);
					  SDK.getBOAPI().update("BO_EU_CPQ_SYSTEM_PRICE", oldBo);
					  
					}else {																								
						BO bo=new BO();
						bo.set("XTMC", rowMap.getString("XTMC"));
						bo.set("XTMH", rowMap.getString("XTMH"));
						bo.set("PX", px.get(rowMap.getString("XTMC")));
						bo.set("CLF",clf);
						bo.set("RGF", rgf);
						bo.set("WXWG",wx);
						bo.set("CGF", cgf);
						bo.set("DJ",dj);
	                    lists.add(bo);
					}
				
				}
				//判断定制信息中，系统名称在模块配置中没有，在单栋设备价格汇总中存在，则进行更新，如果都不存在则进行创建
				for (String key : dzMap.keySet()) {
					if(mapOld.containsKey(key)&&!xtMap.containsKey(key)) {
						BO oldBo=mapOld.get(key);
						  RowMap	rowMap	=dzMap.get(key);
						  oldBo.set("XTMC", rowMap.getString("XTMC"));
						  oldBo.set("XTMH", rowMap.getString("XTMH"));
						  oldBo.set("PX", px.get(rowMap.getString("XTMC")));	
						  String clf=rowMap.getString("CLF").isEmpty()?"0":rowMap.getString("CLF");
						  String rgf=rowMap.getString("RGF").isEmpty()?"0":rowMap.getString("RGF");
						  String wx=rowMap.getString("WX").isEmpty()?"0":rowMap.getString("WX");
					      String cgf=rowMap.getString("CGF").isEmpty()?"0":rowMap.getString("CGF");
						  String dj=rowMap.getString("DJ").isEmpty()?"0":rowMap.getString("DJ");
						  oldBo.set("CLF",clf);
						  oldBo.set("RGF",rgf);
						  oldBo.set("WXWG", wx);
						  oldBo.set("CGF", cgf);
						  oldBo.set("DJ", dj);
						  String zzxs=oldBo.getString("ZZXS").isEmpty()?"0":oldBo.getString("ZZXS");//自制系数
						  String wgxs=oldBo.getString("WGXS").isEmpty()?"0":oldBo.getString("WGXS");//外购系数
						  BigDecimal clfnum=new BigDecimal(clf);	
						  BigDecimal rgfnum=new BigDecimal(rgf);		
						  BigDecimal wxfnum=new BigDecimal(wx);	
						  BigDecimal cgfnum=new BigDecimal(cgf);
						  BigDecimal zznum=new BigDecimal(zzxs);
						  BigDecimal wgnum=new BigDecimal(wgxs);
						  BigDecimal zdj=clfnum.add(rgfnum).multiply(zznum).add(wxfnum.add(cgfnum).multiply(wgnum));
						  oldBo.set("BJDJG", zdj);
						  SDK.getBOAPI().update("BO_EU_CPQ_SYSTEM_PRICE", oldBo);
					  }else if (!mapOld.containsKey(key)&&!xtMap.containsKey(key)&&px.containsKey(key)) {
						  BO bo=new BO();
						  RowMap	rowMap	=dzMap.get(key);
						  String clf=rowMap.getString("CLF").isEmpty()?"0":rowMap.getString("CLF");
						  String rgf=rowMap.getString("RGF").isEmpty()?"0":rowMap.getString("RGF");
						  String wx=rowMap.getString("WX").isEmpty()?"0":rowMap.getString("WX");
					      String cgf=rowMap.getString("CGF").isEmpty()?"0":rowMap.getString("CGF");
						  String dj=rowMap.getString("DJ").isEmpty()?"0":rowMap.getString("DJ");
							bo.set("XTMC", rowMap.getString("XTMC"));
							bo.set("XTMH", rowMap.getString("XTMH"));
							bo.set("PX", px.get(rowMap.getString("XTMC")));
							bo.set("CLF",clf);
							bo.set("RGF", rgf);
							bo.set("WXWG",wx);
							bo.set("CGF", cgf);
							bo.set("DJ",dj);
		                    lists.add(bo);
					}
				}
				
				
				  
				
				
				BO data=SDK.getBOAPI().query("BO_EU_CPQ_PROJECT_MSG").bindId(bindId).detail();
				//获取运输费和安装费
				String   azf=data.getString("AZFZJ");//安装费
				String   ysf=data.getString("YSFZJJG");//运输费
				if(mapOld.containsKey("安装费")) {
				BO bo=	mapOld.get("安装费");
				bo.set("XTMC", "安装费");
				bo.set("PX", "100");
				bo.set("DJ", azf);
				String wgxs=bo.getString("WGXS").isEmpty()?"0":bo.getString("WGXS");//外购系数	
				BigDecimal aznum=new BigDecimal(azf);	
				BigDecimal wgnum=new BigDecimal(wgxs);	
				BigDecimal zdj=aznum.multiply(wgnum);
				bo.set("BJDJG", zdj);
				SDK.getBOAPI().update("BO_EU_CPQ_SYSTEM_PRICE", bo);
				}else {
					BO azBo=new BO();
					azBo.set("XTMC", "安装费");
					azBo.set("PX", "100");
					azBo.set("DJ", azf);
					lists.add(azBo);
				}
				if(mapOld.containsKey("运输费")) {
					BO bo=	mapOld.get("运输费");
					bo.set("XTMC", "运输费");
					bo.set("PX", "200");
					bo.set("DJ", ysf);
					String wgxs=bo.getString("WGXS").isEmpty()?"0":bo.getString("WGXS");//外购系数
					BigDecimal ysnum=new BigDecimal(ysf);	
					BigDecimal wgnum=new BigDecimal(wgxs);	
					BigDecimal zdj=ysnum.multiply(wgnum);
					bo.set("BJDJG", zdj);
					SDK.getBOAPI().update("BO_EU_CPQ_SYSTEM_PRICE", bo);
				}else {
					BO ysBo=new BO();
					ysBo.set("XTMC", "运输费");
					ysBo.set("PX", "200");
					ysBo.set("DJ", ysf);				
					lists.add(ysBo);
				}
				//删除多余的系统信息；
				for (String key : mapOld.keySet()) {
					if(!key.equals("安装费")&&!key.equals("运输费")) {
						if(names.indexOf(key)<0) {
							BO bo=mapOld.get(key);
							SDK.getBOAPI().remove("BO_EU_CPQ_SYSTEM_PRICE",bo.getId());
						}	
					}
					
				}	
				
				SDK.getBOAPI().create("BO_EU_CPQ_SYSTEM_PRICE", lists, bindId, userContext.getUID());
			}
			
			ResponseObject ro = ResponseObject.newOkResponse();
			   return ro.toString();
		} catch (Exception e) {
			throw e;
		}
		
	}

}
